package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import bus.StudentResultBus;
import dto.StudentResultDto;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;

public class JFrameStudentViewResult extends JFrame {

	private JPanel contentPane;
	private JTextField txtSubjectId;
	private JTable tblResult;
	private DefaultTableModel model = new DefaultTableModel();
	// private JFrameStudentFindSubjectId jFrameStudentFindSubjectId;
	private JDialogStudentFindSubjectId jDialogStudentFindSubjectId;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrameStudentViewResult frame = new JFrameStudentViewResult();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFrameStudentViewResult() {
		addWindowFocusListener(new WindowFocusListener() {
			public void windowGainedFocus(WindowEvent arg0) {
				try {
					txtSubjectId.setText(jDialogStudentFindSubjectId.subjectId);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}

			public void windowLostFocus(WindowEvent arg0) {
			}
		});
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				JFrameStudentMainPage jFrameStudentMainPage = new JFrameStudentMainPage();
				jFrameStudentMainPage.setVisible(true);
			}
		});
		setTitle("Student - View result ");
		setResizable(false);
		setBounds(100, 100, 667, 426);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblEnter = new JLabel("Enter subject or quiz");
		lblEnter.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblEnter.setBounds(39, 11, 531, 24);
		contentPane.add(lblEnter);

		JLabel lblNewLabel = new JLabel("Subject ID:");
		lblNewLabel.setBounds(39, 46, 78, 14);
		contentPane.add(lblNewLabel);

		txtSubjectId = new JTextField();
		txtSubjectId.setBounds(114, 43, 178, 20);
		contentPane.add(txtSubjectId);
		txtSubjectId.setColumns(10);
		txtSubjectId.getDocument().addDocumentListener(new DocumentListener() {
			public void changedUpdate(DocumentEvent e) {
				// text was changed

			}

			public void removeUpdate(DocumentEvent e) {
				// text was deleted
				searchResult();
			}

			public void insertUpdate(DocumentEvent e) {
				// text was inserted

				searchResult();
			}
		});

		JButton btnFindSubjectId = new JButton("...");
		btnFindSubjectId.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				jDialogStudentFindSubjectId = new JDialogStudentFindSubjectId();
				jDialogStudentFindSubjectId.setVisible(true);
			}
		});
		btnFindSubjectId.setBounds(302, 42, 23, 23);
		contentPane.add(btnFindSubjectId);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 106, 641, 280);
		contentPane.add(scrollPane);

		tblResult = new JTable();
		model = new DefaultTableModel(new Object[][] {}, new String[] {
				"Quiz no", "Marks", "Comment", "Description" }) {
			Class[] columnTypes = new Class[] { Integer.class, Integer.class,
					String.class, String.class, String.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		};
		tblResult.setModel(model);

		tblResult.getColumnModel().getColumn(0).setPreferredWidth(74);
		scrollPane.setViewportView(tblResult);

		JLabel lblResult = new JLabel("Result:");
		lblResult.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblResult.setBounds(10, 82, 531, 24);
		contentPane.add(lblResult);
	}

	private void searchResult() {

		ArrayList<StudentResultDto> listResultDtos = StudentResultBus
				.getResultBySubject(JFrameStudentMainPage.ID,
						txtSubjectId.getText());
		int i = 0;
		for (StudentResultDto result : listResultDtos) {

			String[] newTableColumn = { String.valueOf(result.getQuizNo()),
					result.getMark(), result.getComment(),
					result.getResultDesc() };
			model.addRow(newTableColumn);
			i++;
		}

	}
}
